package com.zeng.sys.filemd.servicemodule.service.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.sys.filemd.servicemodule.entity.FileRecord;
import com.zeng.sys.filemd.servicemodule.mapper.FileRecordMapper;
import com.zeng.sys.filemd.servicemodule.service.FileRecordService;
import com.zeng.sys.filemd.servicemodule.service.FileServerService;
import com.zeng.sys.filemd.servicemodule.transfer.FileHttpTransfer;
import com.zeng.sys.filemd.servicemodule.util.MultipartFileUtil;
import com.zeng.sys.filemd.servicemodule.vo.FileRecordVo;
import com.zeng.sys.filemd.servicemodule.vo.FileServerVo;
import com.zeng.sys.filemd.servicemodule.wrapperImpl.FileRecordWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class FileRecordServiceImpl extends ServiceImpl<FileRecordMapper, FileRecord> implements FileRecordService {

    @Autowired
    FileRecordMapper fileRecordMapper;

    @Autowired
    MultipartFileUtil multipartFileUtil;

    @Autowired
    FileServerService fileServerService;

    private FileRecordWrapper fileRecordWrapper = new FileRecordWrapper();

    @Override
    public FileRecordVo addOne(FileRecordVo fileRecordVo) {
        FileRecord entity = fileRecordVo.convertToFileRecord();
        save(entity);
        return FileRecordVo.convertToFileRecordVo(entity);
    }

    @Override
    public FileRecordVo getOneByCode(String code) {
        return FileRecordVo.convertToFileRecordVo(getById(code));
    }

    @Override
    public Integer update(FileRecordVo fileRecordVo) {
        return this.fileRecordMapper.updateById(fileRecordVo.convertToFileRecord());
    }

    @Override
    public Integer delete(String code) {
        return this.fileRecordMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {
        return this.fileRecordMapper.deleteBatchIds(codes);
    }

    @Override
    public List<FileRecordVo> findPage(Page page, FileRecordVo fileRecordVo) {
        this.fileRecordMapper.selectPage(page, fileRecordWrapper.getWrapper(fileRecordVo));
        List<FileRecordVo> list = (List<FileRecordVo>)FileRecordVo.listFileRecordVo(page.getRecords());
        page.setRecords(null);
        return list;
    }

    @Override
    public Integer count(FileRecordVo fileRecordVo) {
        return this.fileRecordMapper.selectCount(fileRecordWrapper.getWrapper(fileRecordVo));
    }

    @Override
    public List<FileRecordVo> findAll(FileRecordVo fileRecordVo) {
        return FileRecordVo.listFileRecordVo(list(fileRecordWrapper.getWrapper(fileRecordVo)));
    }

    @Override
    public Integer saveBatch(List<FileRecordVo> fileRecordVos) {
        saveBatch(FileRecordVo.listFileRecord(fileRecordVos));
        return fileRecordVos.size();
    }

    @Override
    public boolean updateOrSaveBatch(List<FileRecordVo> fileRecordVos) {
        return saveOrUpdateBatch(FileRecordVo.listFileRecord(fileRecordVos));
    }

    @Override
    public boolean updateBatch(List<FileRecordVo> fileRecordVos) {
        return updateBatchById(FileRecordVo.listFileRecord(fileRecordVos));
    }

    @Override
    public ResponseUtil deleteRecordAndFile(String code) {
        FileRecordVo oneByCode = this.getOneByCode(code);
        if (oneByCode != null) {
            multipartFileUtil.handleFileRecordVoOnServer(oneByCode);
            FileHttpTransfer fileHttpTransfer = new FileHttpTransfer(oneByCode.getFileServerVo());
            ResponseUtil delete = fileHttpTransfer.delete(oneByCode);
            if (delete!= null && delete.getStatus() == 200) {
                return ResponseUtil.success("操作成功").addData("deleteCount", this.delete(code));
            }
        }
        return ResponseUtil.serviceError("删除失败");
    }

    @Override
    public ResponseUtil deleteRecordsAndFiles(List<String> codes) {
        if (codes == null || codes.size() <= 0) {
            return ResponseUtil.badRequest("没有选择资源");
        }
        Map<String, FileHttpTransfer> mapService = new HashMap<>();
        List<FileRecordVo> fileRecordCodes = this.findAll(FileRecordVo
                .builder()
                .codes(codes)
                .build());
        Set<String> serverCodes = fileRecordCodes.stream().map(FileRecordVo::getFileServerCode).collect(Collectors.toSet());
        List<FileServerVo> servers = fileServerService.findAll(FileServerVo.builder().codes(serverCodes).build());
        for (FileServerVo fileServerVo : servers) {
            FileHttpTransfer fileHttpTransfer = new FileHttpTransfer(fileServerVo);
            mapService.put(fileServerVo.getCode(), fileHttpTransfer);
        }
        ResponseUtil responseUtil = null;
        for (String s : mapService.keySet()) {
            List<String> filePath = fileRecordCodes.stream().filter((e) -> e.getFileServerCode().equals(s)).map(FileRecordVo::getPath).collect(Collectors.toList());
            List<String> recordCodes = fileRecordCodes.stream().filter((e) -> e.getFileServerCode().equals(s)).map(FileRecordVo::getCode).collect(Collectors.toList());
            responseUtil = mapService.get(s).batchDelete(filePath);
            if (responseUtil!= null && responseUtil.getStatus() == 200) {
                batchDelete(recordCodes);
            }
        }
        return responseUtil;

    }

}
